<?php

set_include_path(dirname(__FILE__) . "/library" . PATH_SEPARATOR . get_include_path());
require_once "Zend/XmlRpc/Client.php";		
		
		class Calculator {
			
			//E-Goi Errors:
			const SUBSCRIBER_NOT_FOUND = "SUBSCRIBER_NOT_FOUND";
			const SUBSCRIBER_MISSING = "SUBSCRIBER_MISSING";
			
			var $apiKey = "97f0b9aab8ce206e9b6810ae0cb871aab531212e";
			var $listID = 44;
			
			//variaveis do egoi (campos basicos da lista MyNutrition24 )
			protected $NOME;
			protected $APELIDO;
			protected $EMAIL;
			protected $MOBILE;
			protected $DDN;			
			
			function __construct() {
			}
			
			protected function validateUser() {
				
				$params = array(	"apikey"    	=> $this->apiKey,
                    				"listID"    	=> $this->listID,
                    				"subscriber"    => $this->EMAIL);
									
				$client = new Zend_XmlRpc_Client("http://api.e-goi.com/v2/xmlrpc.php");
				$result = $client->call("subscriberData", array($params));
				
				if($result["ERROR"] == SUBSCRIBER_NOT_FOUND || $result["ERROR"] == SUBSCRIBER_MISSING) $this->addNewUser();
			}
			
			function addNewUser() {
				
				echo "Creating new user";
				
				$params = array( 	"apikey"    	=> $this->apiKey,
									"listID"	 	=> $this->listID,
									"email"			=> $this->EMAIL);
				
				$client = new Zend_XmlRpc_Client("http://api.e-goi.com/v2/xmlrpc.php");
				$result = $client->call("addSubscriber", array($params));
			}
			
			function editUser($paramsIn) {
				
				/**
				* TODO: 
				* Resolver o problema do birht_date
				*/
				$params = array( 	"apikey"    	=> $this->apiKey,
									"listID"	 	=> $this->listID,
									"subscriber"    => $this->EMAIL,
									"email"			=> $this->EMAIL,
									"cellphone"		=> $this->MOBILE,
									"first_name"	=> $this->NOME,
									"last_name"		=> $this->APELIDO,
									"birth_date"	=> date("Y/m/d"),
									"extra_472"		=> $paramsIn->IMC,
									"extra_479"		=> $paramsIn->KG_META);
								
				$client = new Zend_XmlRpc_Client("http://api.e-goi.com/v2/xmlrpc.php");
				$result = $client->call("editSubscriber", array($params));
			}
		} 
		
		class IMCCalculator extends Calculator {
			
			var $IMC;
			var $PESO_A_PERDER;
			var $PESO_A_GANHAR;
			var $PESO;
			var $ALTURA;
			var $SEXO;
			var $BIOTIPO;
			var $KG_META;
			var $PESO_IDEAL;
			
			function __construct() {
			}
			
			function toEGoi($params) {
				
				$this->NOME = $params->NOME;
				$this->APELIDO = $params->APELIDO;
				$this->PESO = $params->PESO;
				$this->SEXO = $params->SEXO;
				$this->BIOTIPO = $params->BIOTIPO;
				$this->DDN = $params->DNN;
				$this->KG_META = $params->KG_META;
				
				$this->EMAIL = $params->EMAIL;
				$this->DISTRITO = $params->DISTRITO;
				$this->PHONE = $params->PHONE;
				
				$this->validateUser();
				
				$paramsOut = (object) array( 	"NOME"    		=> $this->NOME,
												"APELIDO"	 	=> $this->APELIDO,
												"PESO"			=> $this->PESO,
												"SEXO"	 		=> $this->SEXO,
												"BIOTIPO"	 	=> $this->BIOTIPO,
												"DDN"	 		=> $this->DDN,
												"KG_META"	 	=> $this->KG_META,
												"DISTRITO"	 	=> $this->DISTRITO,
												"PHONE"	 		=> $this->PHONE);
				
				
				$this->editUser($paramsOut);
			}
			
			/**
			* TODO:
			*/
			function getData() {
				return NULL;
			}
		}
?>